package org.docmanagement.commons.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class GenericServiceImpl<T> extends HibernateDaoSupport implements GenericService<T> {

	public GenericServiceImpl(SessionFactory sessionFactory) {
		super();
		setSessionFactory(sessionFactory);
	}

	protected abstract Class<T> getObjectClass();

	public Integer add(T o) throws ServiceException {
		return (Integer) getHibernateTemplate().save(o);
	}

	public T findById(Integer id) throws ServiceException {
		return (T) getHibernateTemplate().get(getObjectClass(), id);
	}

	public void update(T o) throws ServiceException {
		getHibernateTemplate().update(o);
	}

	public void removeById(Integer id) throws ServiceException {
		remove(findById(id));
	}

	public void remove(T objectToRemove) throws ServiceException {
		// On supprime la categorie
		getHibernateTemplate().delete(objectToRemove);
	}

	public List<T> findAll() throws ServiceException {
		return getHibernateTemplate().loadAll(getObjectClass());
	}
}
